package com.hl.toolkit.framework.aspect;

import com.anpotech.tms.app.BaseResult;
import com.anpotech.tms.app.ErrorCode;
import com.anpotech.tms.framework.annotation.LoginCheckAnnotation;
import com.anpotech.tms.framework.core.util.SessionUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Aspect
@Component
public class LoginCheckAspect {

	private static final Log logger = LogFactory.getLog(LoginCheckAspect.class);


	@Around(value = "@annotation(loginCheck)")
	public Object checkLogin(ProceedingJoinPoint pjd, LoginCheckAnnotation loginCheck) throws Throwable {
		BaseResult baseResult=new BaseResult();
		baseResult.setMessage("未登陆");
		baseResult.setCode(ErrorCode.UNLOGIN);
		String signature = pjd.getSignature().toString();
		logger.info("待执行方法" + signature + "参数为" + Arrays.toString(pjd.getArgs()));
		for (Object o : pjd.getArgs()) {
			if (o instanceof HttpServletRequest){
				if (SessionUtil.isLogin(((HttpServletRequest)o).getSession())) {
					baseResult = (BaseResult) pjd.proceed();
				}
			}
		}

		// TODO: 2017/11/8 0008  视时替换成token

		return baseResult;
	}
}
